package com.turnengine.client.local.mobile.bean;

import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import java.io.IOException;

/**
 * The Mobile Serializer.
 */
public class MobileSerializer implements IMobileSerializer {

	@Override
	public IMobile readObject(IDataReader reader) throws IOException {

		// Read fields
		int id = reader.readInt();
		String name = reader.readString(true);
		int playerId = reader.readInt();
		int originId = reader.readInt();
		int destinationId = reader.readInt();

		// Create object
		IMobile object = new Mobile();
		object.setId(id);
		object.setName(name);
		object.setPlayerId(playerId);
		object.setOriginId(originId);
		object.setDestinationId(destinationId);
		return object;
	}

	public void writeObject(IDataWriter writer, IMobile object) throws IOException {

		// Get fields
		int id = object.getId();
		String name = object.getName();
		int playerId = object.getPlayerId();
		int originId = object.getOriginId();
		int destinationId = object.getDestinationId();

		// Write fields
		writer.writeInt(id);
		writer.writeString(name, true);
		writer.writeInt(playerId);
		writer.writeInt(originId);
		writer.writeInt(destinationId);
	}
}
